package com.slmxz.tms.document.controller;

import com.slmxz.tms.common.model.Query;
import com.slmxz.tms.common.result.PageResult;
import com.slmxz.tms.common.result.Result;
import com.slmxz.tms.common.support.Condition;
import com.slmxz.tms.document.model.entity.Cargo;
import com.slmxz.tms.document.model.vo.CargoVO;
import com.slmxz.tms.document.service.CargoService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 货物信息前端控制层
 * @author sunyao
 * @Date 2024-11-15
 */
@Tag(name = "货物信息接口")
@RestController
@RequestMapping("/document/cargo")
@RequiredArgsConstructor
public class CargoController  {

    private final CargoService cargoService;

    @Operation(summary = "货物信息分页列表")
    @GetMapping("/page")
    public PageResult<?> selectCargoPage(Query query, CargoVO cargo) {
        IPage<CargoVO> result = this.cargoService.selectCargoPage(Condition.getPage(query),cargo);
        return PageResult.success(result);
    }

    @Operation(summary = "新增货物信息")
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPerm('document:cargo:add')")
    public Result<Void> saveCargo(@RequestBody @Valid CargoVO  cargo) {
        boolean result = this.cargoService.saveCargo(cargo);
        return Result.result(result);
    }

    @Operation(summary = "获取货物信息详情数据")
    @GetMapping("/detail/{id}")
    @PreAuthorize("@ss.hasPerm('document:cargo:edit')")
    public Result<Cargo> selectCargoDetail(
        @Parameter(description = "货物信息ID") @PathVariable Long id
    ) {
        Cargo cargo = this.cargoService.selectCargoDetail(id);
        return Result.success(cargo);
    }

    @Operation(summary = "修改货物信息")
    @PutMapping(value = "/update")
    @PreAuthorize("@ss.hasPerm('document:cargo:edit')")
    public Result<Void> updateCargo(
            @RequestBody @Validated Cargo cargo
    ) {
        boolean result = cargoService.updateCargo(cargo);
        return Result.result(result);
    }

    @Operation(summary = "删除货物信息")
    @DeleteMapping("/remove/{ids}")
    @PreAuthorize("@ss.hasPerm('document:cargo:delete')")
    public Result<Void> deleteCargos(
        @Parameter(description = "货物信息ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = this.cargoService.deleteCargos(ids);
        return Result.result(result);
    }

    @Operation(summary = "激活或者注销货物信息")
    @PostMapping("/activeOrLogout")
    @PreAuthorize("@ss.hasPerm('document:business:delete')")
    public Result<Void> activeOrLogout(@RequestBody CargoVO cargo) {
        boolean result = this.cargoService.activeOrLogout(cargo);
        return Result.result(result);
    }
}
